import 'package:hongsheng/api/SPClassApiManager.dart';
import 'package:hongsheng/api/SPClassHttpCallBack.dart';
import 'package:hongsheng/app/SPClassApplicaion.dart';
import 'package:hongsheng/model/SPClassBaseModelEntity.dart';
import 'package:hongsheng/utils/SPClassCommonMethods.dart';
import 'package:hongsheng/utils/SPClassImageUtil.dart';
import 'package:hongsheng/utils/SPClassToastUtils.dart';
import 'package:hongsheng/utils/colors.dart';
import 'package:hongsheng/widgets/SPClassToolBar.dart';
import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///兑换--填写信息页
class SPClassInputInfo extends StatefulWidget {
  final  data;
  const SPClassInputInfo({Key ?key,this.data,}) : super(key: key);

  @override
  _SPClassInputInfoState createState() => _SPClassInputInfoState();
}

class _SPClassInputInfoState extends State<SPClassInputInfo> {
  TextEditingController _nameController = new TextEditingController();
  TextEditingController _phoneController = new TextEditingController();
  TextEditingController _addressController = new TextEditingController();
  TextEditingController _detailController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: SPClassToolBar(
        context,title: "填写信息",),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: width(10),right: width(10),top: width(10)),
              padding: EdgeInsets.symmetric(horizontal: width(15),vertical: width(12)),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
              ),
              child:Row(
                children: <Widget>[
                  Image.network(
                    widget.data.spProIcon,
                    fit: BoxFit.cover,
                    width: width(77),
                  ),
                  SizedBox(width: width(12),),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text('${widget.data.spProProductName}',style: TextStyle(fontSize: sp(17),),),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(widget.data.spProType=='3'?'':'${widget.data.spProPrice}积分',style: TextStyle(fontSize: sp(13),color: MyColors.main1),),
                            Text('x1',style: TextStyle(fontSize: sp(13),color: MyColors.grey_99),),
                          ],
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              margin: EdgeInsets.only(left: width(10),right: width(10),top: width(10)),
              padding: EdgeInsets.symmetric(horizontal: width(15),vertical: width(12)),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
              ),
              child: widget.data.spProProductType=='1'?Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('发货规则',style: TextStyle(fontSize: sp(13),fontWeight: FontWeight.bold),),
                  Text('1、确认地址后将不能修改，请确认地址是否准确',style: TextStyle(fontSize: sp(12),color: MyColors.grey_66)),
                  Text('2、确认地址后需要修改地址请联系客服',style: TextStyle(fontSize: sp(12),color: MyColors.grey_66)),
                ],
              ):
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('特别注意',style: TextStyle(fontSize: sp(13),fontWeight: FontWeight.bold),),
                  Text('1、请确认手机号码是否正确，避免充值错误造成损失',style: TextStyle(fontSize: sp(12),color: MyColors.grey_66)),
                  Text('2、需要修改充值号码请联系客服，如充值已经完成，则不允许修改',style: TextStyle(fontSize: sp(12),color: MyColors.grey_66)),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: width(10),right: width(10),top: width(10)),
              padding: EdgeInsets.symmetric(horizontal: width(15),vertical: width(12)),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
              ),
              child: widget.data.spProProductType=='1'?Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Text('    收货人:',style: TextStyle(fontSize: sp(13)),),
                      SizedBox(
                        width: width(10),
                      ),
                      Expanded(
                        child: TextField(
                          controller: _nameController,
                          decoration: InputDecoration(
                            hintText: '请输入名字',
                            hintStyle: TextStyle(fontSize: sp(13),color: MyColors.grey_cc),
                            border: InputBorder.none,
                          ),
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: width(10),
                  ),
                  Row(
                    children: <Widget>[
                      Text('手机号码:',style: TextStyle(fontSize: sp(13),),),
                      SizedBox(
                        width: width(10),
                      ),
                      Expanded(
                        child: TextField(
                          controller: _phoneController,
                          maxLength: 11,
                          keyboardType: TextInputType.number,
                          decoration: InputDecoration(
                              hintText: '请输入号码',
                              counterText: '',
                              hintStyle: TextStyle(fontSize: sp(13),color: MyColors.grey_cc),
                            border: InputBorder.none,
                          ),
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: width(10),
                  ),
                  Row(
                    children: <Widget>[
                      Text('所在地区:',style: TextStyle(fontSize: sp(13),),),
                      SizedBox(
                        width: width(10),
                      ),
                      Expanded(
                        child: TextField(
                          readOnly: true,
                          onTap: (){
                            selectCity();
                          },
                          controller: _addressController,
                          decoration: InputDecoration(
                              hintText: '省、市、区、街道',
                              counterText: '',
                              hintStyle: TextStyle(fontSize: sp(13),color: MyColors.grey_cc),
                            border: InputBorder.none,
                          ),
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: width(10),
                  ),
                  Row(
                    children: <Widget>[
                      Text('详细地址:',style: TextStyle(fontSize: sp(13),),),
                      SizedBox(
                        width: width(10),
                      ),
                      Expanded(
                        child: TextField(
                          controller: _detailController,
                          decoration: InputDecoration(
                              hintText: '小区/乡村',
                              counterText: '',
                              hintStyle: TextStyle(fontSize: sp(13),color: MyColors.grey_cc),
                            border: InputBorder.none,
                          ),
                        ),
                      )
                    ],
                  ),
                ],
              ): Row(
                children: <Widget>[
                  Text('手机号码:',style: TextStyle(fontSize: sp(13),),),
                  SizedBox(
                    width: width(10),
                  ),
                  Expanded(
                    child: TextField(
                      controller: _phoneController,
                      maxLength: 11,
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        hintText: '请输入号码',
                        counterText: '',
                        hintStyle: TextStyle(fontSize: sp(13),color: MyColors.grey_cc),
                        border: InputBorder.none,
                      ),
                    ),
                  )
                ],
              ),

            ),
            GestureDetector(
              onTap: (){
                if (_phoneController.text.length != 11) {
                  SPClassToastUtils.spFunShowToast(
                      msg: "请输入正确11位手机号码!");
                  return;
                }
                Map<String,dynamic> params ={};

                if(widget.data.spProType=='3'){
                  params = {
                    'product_id':widget.data.spProProductId.toString(),
                    'phone':_phoneController.text,
                    'real_name':_nameController.text,
                    'address':'${_addressController.text}${_detailController.text}',
                    'phase_num':widget.data.spProPhaseNum,
                  };
                  SPClassApiManager.spFunGetInstance().spFunCreateLootOrder<SPClassBaseModelEntity>(context: context,params: params,spProCallBack:SPClassHttpCallBack(
                      spProOnSuccess: (result){
                        showSuccessToast();
                      }
                  ));
                }else{
                  params = {
                    'product_id':widget.data.spProProductId.toString(),
                    'phone':_phoneController.text,
                    'real_name':_nameController.text,
                    'address':'${_addressController.text}${_detailController.text}',
                  };
                  SPClassApiManager.spFunGetInstance().spFunPointsCreateOrder<SPClassBaseModelEntity>(context: context,params: params,spProCallBack:SPClassHttpCallBack(
                      spProOnSuccess: (result){
                        SPClassApplicaion.spFunGetUserInfo();
                        showSuccessToast();
                      }
                  ));
                }

              },
              child: Container(
                width: width(164),
                height: width(30),
                margin: EdgeInsets.only(top: width(30),bottom:width(30) ),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: MyColors.main1,
                  borderRadius: BorderRadius.circular(150)
                ),
                child: Text('确认',style: TextStyle(fontSize: sp(13),color: Colors.white),),
              ),
            )
          ],
        ),
      ),
    );
  }

  selectCity()async{
    FocusScope.of(context).requestFocus(FocusNode());
    Result? tempResult =await CityPickers.showCityPicker(
        context: context,
        height: width(200)
    );
    _addressController.text = '${tempResult?.provinceName}${tempResult?.cityName}${tempResult?.areaName}';
    setState(() {

    });
  }

  showSuccessToast(){
    showDialog(context: context,builder: (context){
      return WillPopScope(
        child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width,
                margin: EdgeInsets.symmetric(horizontal: width(44)),
                padding: EdgeInsets.symmetric(vertical: width(15)),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Column(
                  children: <Widget>[
                    Image.asset(
                      SPClassImageUtil.spFunGetShopImagePath(
                          "ic_success"),
                      width: width(46),
                    ),
                    SizedBox(height: width(10),),
                    Text('兑换成功',style: TextStyle(fontSize: sp(17),),),
                    SizedBox(height: width(4),),
                    Text('最快24小时内发货，如有节假日可能推迟',style: TextStyle(fontSize: sp(12),color: MyColors.grey_99),),
                    SizedBox(height: width(30),),
                    GestureDetector(
                      onTap: (){
                        Navigator.pop(context);
                        Navigator.pop(context);
                      },
                      child: Text('知道了',style: TextStyle(fontSize: sp(17),color: MyColors.main1),),
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
        onWillPop:() async{
          return false;
        },
      );
    });
  }

}
